﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;


namespace ConsoleApplication1
{
  //--------------SERVER------------------
    public class Program
    {
        static private byte[] buffer = new byte[10*1024*1024];
        static Socket client;
        static List<Socket> lst = new List<Socket>();
        static List<Thread> Th  = new List<Thread>();
        static void Main(string[] args)
        {
            TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 8060);
            server.Start();
            Console.WriteLine("Server has started...");
            
            while (true)
            {
                client = server.AcceptSocket();
                lst.Add(client);
                Thread temp = new Thread(delegate() { Request(client); });
                Th.Add(temp);
                temp.Start();
            }
        }
        static void Request(Socket mySocket) 
        {
            try
            {
                NetworkStream stream = new NetworkStream(mySocket);
                string message = "";
                while (!stream.DataAvailable)
                {
                    Thread.Sleep(50);
                }
                while (stream.DataAvailable)
                {
                    int i = mySocket.Available;
                        byte[] msg = new byte[i];
                        mySocket.Receive(msg);
                        message += Encoding.UTF8.GetString(msg);
                    
                }
                Console.WriteLine(message);
                string[] request = message.Split(';');

                string answer = "";
                switch (request[0])
                {
                    case "SENDFILE":
                        {
                            int size1 = stream.Read(buffer, 0, 10*1024 * 1024);
                            byte[] file = new byte[size1];
                            for (int x = 0; x < size1; x++)
                            {
                                file[x] = buffer[x];
                            }
                                answer = DS_CSV_server.SafeFile(request[1], file);
                            break;
                        }
                    case "CREATE":
                        {
                            string person = "";
                            for (int i = 1; i < request.Length; i++)
                            {
                                person += request[i];
                                if (i < request.Length - 1) { person += ";"; }
                            }
                                answer = DS_CSV_server.Create(person);
                            break;
                        }
                    case "READ":
                        {
                            answer = DS_CSV_server.Read();
                            break;
                        }
                    case "UPDATE":
                        {
                            string person = "";
                            for (int i = 2; i < request.Length; i++)
                            {
                                person += request[i];
                                if (i < request.Length - 1) { person += ";"; }
                            }
                            answer = DS_CSV_server.Update(Convert.ToInt32(request[1]), person);
                            break;
                        }
                    case "DELETE":
                        {
                            answer = DS_CSV_server.Delete(Convert.ToInt32(request[1]));
                            break;
                        }
                    case "CLEAR":
                        {
                            answer = DS_CSV_server.Clear();
                            break;
                        }
                    default:
                        {
                            Console.WriteLine("команда не распознана!"); 
                            answer = "fail";
                            break;
                        }
                }

                // когда вычитали отправить ответ 

                //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                Thread.Sleep(50);
                byte[] text = Encoding.UTF8.GetBytes(answer);
                mySocket.Send(text);
            }
            catch (Exception e)
            {
                Console.WriteLine("EXCEPTION OCCURED: "+e.Message); 
            }
            finally
            {
                mySocket.Close();
                lst.Remove(mySocket);
                Th.Remove(Thread.CurrentThread);
                Thread.CurrentThread.Abort();
            }
        }
    }
}
